package netexpwithoutui.Client;

import domains.Users;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author win_d
 */
public class ClientThread extends Thread {

    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    private Users user;

    public ClientThread(Users user) throws IOException {
        this.user = user;
        if (this.user.getStatus().equals('y')) {
            this.socket = new Socket(this.user.getIp(), 8090);
        } else {
            this.socket = new Socket("0.0.0.0", 8090);
        }
        this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
        this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())), true);
    }

    public void SendMessage(String message) {
        if (socket.isClosed()) {
            return;
        }
        try {
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String date = sDateFormat.format(new java.util.Date());
            message.replaceAll("@", "@!");
            message.replaceAll("$", "@#");
            String MessageInfo = null;
            MessageInfo = "${name}" + this.user.getLoginName() + "${date}" + date + "${message}" + message+"#{end}\n";
            out.write(MessageInfo);
            out.flush();
        } catch (Exception e) {
            return;
        }
    }

    public void ReceiveMessage(InputStream in) {
        while (!socket.isClosed()) {
            String messageInfo = null;
            BufferedReader input = new BufferedReader(new InputStreamReader(in));
            try {
                messageInfo = input.readLine();
                messageInfo.replaceAll("@#", "$");
                messageInfo.replaceAll("@!", "@");
                String loginName = messageInfo.substring(messageInfo.indexOf("${name}") + 6,
                        messageInfo.indexOf("${date}"));
                System.out.print(loginName);
                String dataTime=messageInfo.substring(messageInfo.indexOf("${date}")+19,
                        messageInfo.indexOf("${message}"));
                System.out.println(dataTime+":");
                String message=messageInfo.substring(messageInfo.indexOf("${message}"),
                        messageInfo.indexOf("${end}"));
                System.out.println(message);
            } catch (IOException ex) {
                Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public void run() {
        String message = null;
        InputStream input = null;
        SendMessage(message);
        ReceiveMessage(input);
    }
    
}
